﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 缓存提供者基类。
 * 
 * 最后修改：2011-10-28
 * ************************************************************/
namespace Mozlite.Caching
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Caching;
    using System.Collections;
    /// <summary>
    /// 缓存提供者基类。
    /// </summary>
    public abstract class CacheProviderBase : IDisposable, ICacheProvider
    {
        private static Type cacheSettingAttribute = typeof(CacheSettingsAttribute);
        /// <summary>
        /// 初始化类型<see cref="T:Mozlite.Caching.CacheProviderBase"/>。
        /// </summary>
        protected CacheProviderBase() { }
        /// <summary>
        /// 清楚所有缓存。
        /// </summary>
        public abstract void Clear();
        /// <summary>
        /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
        }
        /// <summary>
        /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
        /// </summary>
        /// <param name="disposing">是否删除资源。</param>
        protected virtual void Dispose(bool disposing)
        {
        }
        /// <summary>
        /// 获取缓存键中的所有缓存列表。
        /// </summary>
        /// <typeparam name="T">缓存对象类型。</typeparam>
        /// <param name="keys">缓存键迭代器实例对象。</param>
        /// <returns>返回缓存列表。</returns>
        public virtual IDictionary<string, T> Get<T>(IEnumerable<string> keys)
        {
            if (keys == null)
            {
                return new Dictionary<string, T>();
            }
            Dictionary<string, T> dictionary = new Dictionary<string, T>();
            IEnumerator<string> enumerator = keys.GetEnumerator();
            while (enumerator.MoveNext())
            {
                string current = enumerator.Current;
                object value = this.Get(current);
                if ((value != null) && (value is T))
                {
                    dictionary.Add(current, (T)value);
                }
            }
            return dictionary;
        }
        /// <summary>
        /// 获取当前键的缓存对象。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <returns>返回缓存对象。</returns>
        public abstract object Get(string key);
        /// <summary>
        /// 获取当前键的缓存对象。
        /// </summary>
        /// <typeparam name="T">缓存类型。</typeparam>
        /// <param name="key">缓存键。</param>
        /// <returns>返回缓存对象。</returns>
        public virtual T Get<T>(string key)
        {
            object obj = this.Get(key);
            if ((obj != null) && (obj is T))
            {
                return (T)obj;
            }
            return default(T);
        }
        /// <summary>
        /// 获取缓存设定实例对象。
        /// </summary>
        /// <param name="value">实例对象。</param>
        /// <returns>返回当前实例的缓存设定实例对象。</returns>
        protected static CacheSettingsAttribute GetCacheSettings(object value)
        {
            if (value != null)
            {
                object[] customAttributes = value.GetType().GetCustomAttributes(cacheSettingAttribute, true);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    return (customAttributes[0] as CacheSettingsAttribute);
                }
            }
            return null;
        }
        /// <summary>
        /// 获取枚举非泛型字典实例对象。
        /// </summary>
        /// <returns>返回枚举非泛型字典实例对象。</returns>
        public abstract IDictionaryEnumerator GetEnumerator();
        /// <summary>
        /// 将所有字典中的缓存项插入到缓存中。
        /// </summary>
        /// <typeparam name="T">缓存类型。</typeparam>
        /// <param name="items">缓存列表实例对象。</param>
        /// <param name="dependencies">缓存依赖项。</param>
        /// <param name="absoluteExpiration">缓存过期绝对日期。</param>
        /// <param name="slidingExpiration">缓存过期相对日期。</param>
        /// <param name="priority">缓存优先级。</param>
        /// <param name="onRemoveCallback">单移除缓存触发的回调。</param>
        public virtual void Insert<T>(IDictionary<string, T> items, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
        {
            if (items == null || items.Count == 0)
                return;
            foreach (var item in items)
            {
                if (item.Value == null)
                    continue;
                this.Insert(item.Key, item.Value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
            }
        }
        /// <summary>
        /// 将对象插入到缓存中。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="value">缓存对象。</param>
        /// <param name="dependencies">缓存依赖项。</param>
        /// <param name="absoluteExpiration">缓存过期绝对日期。</param>
        /// <param name="slidingExpiration">缓存过期相对日期。</param>
        /// <param name="priority">缓存优先级。</param>
        /// <param name="onRemoveCallback">单移除缓存触发的回调。</param>
        public abstract void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
        /// <summary>
        /// 移除缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        public abstract void Remove(string key);
        /// <summary>
        /// 获取缓存提供者名称。
        /// </summary>
        public abstract string ProviderName { get; }
    }
}
